<?php
declare (strict_types=1);

namespace app\command;

use app\common\Redis;
use app\model\Marker as MarkerModel;
use app\model\User;
use app\service\MarkerService;
use app\service\MiniappService;
use app\service\TencentMapService;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\helper\Arr;

class Marker extends Command
{

    protected function configure()
    {
        // 指令配置
        $this->setName('sync')
            ->setDescription('the marker command');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->syncQrcode($input, $output);
    }

    /**
     * 更新标点小程序二维码
     * @param $input
     * @param $output
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function syncQrcode($input, $output)
    {
        $list = MarkerModel::query()->field(['id'])->select();
        foreach ($list as $model) {
            [$isOk, $qrCode] = MiniappService::getUnlimitQr((string)$model->id);
            if (!$isOk) {
                $output->writeln($qrCode);
            }
            // 保存小程序二维码
            [$isOk, $saveRes] = (new MarkerService())->saveQr($qrCode, 0);
            if (!$isOk) {
                $output->writeln($saveRes);
            }
            MarkerModel::update(['qrcode_id' => $saveRes], ['id' => $model->id]);
            $output->writeln("更新成功：$model->id");
            sleep(1);
        }
    }
}
